
/*
 *  Created with IntelliJ IDEA  Community.
 *  Description: taily
 *  Author: mabb
 *  DateTime: 2023-8-16
 *  基础类别模块
 */

package com.example.tally.frag_record;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import com.example.tally.R;
import com.example.tally.db.TypeBean;

import java.util.ArrayList;
import java.util.List;

/**
 * 记录页面的支出模块
 */
public abstract class BaseTypeFragment extends Fragment implements View.OnClickListener {
    EditText typeed;
    ImageView typeIv,type_save;
    GridView typeGv;
    List<TypeBean> typeBeanList;// GridView 的数据源
    TypeBeanSelectAdater typeBeanAdater;
    TypeBean typeBean;//将需要插入到记账本中的数据保存成对象的形式

    Boolean paramBean = false;  //判断是否是值传递过来了
    @Override
    public void onCreate(@Nullable @org.jetbrains.annotations.Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if(!paramBean) {
            typeBean = new TypeBean();
            typeBean.setTypeName("其他");
            typeBean.setsImageId(R.mipmap.ic_qita_fs);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view =  inflater.inflate(R.layout.fragment_type, container, false);
        initView(view);
        // 给GridView填充数据的方法
        loadDataToGridView();
        getGridViewListener();
        return view;

    }

    /*获取GV的点击事件*/
    private void getGridViewListener() {
        typeGv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                typeBeanAdater.selectPos = position;
                typeBeanAdater.notifyDataSetChanged(); //显示绘制
                TypeBean typeBean = typeBeanList.get(position);
                int sImageId = typeBean.getsImageId();
                typeIv.setImageResource(sImageId);
                BaseTypeFragment.this.typeBean.setImageId(typeBean.getImageId());
                BaseTypeFragment.this.typeBean.setsImageId(sImageId);
                BaseTypeFragment.this.typeBean.setKind(typeBean.getKind());
            }
        });
    }

    public void setTypeBean(TypeBean objbean){
        typeBean = objbean;
        paramBean = true;
    }

    void loadDataToGridView() {
        typeBeanList = new ArrayList<>();
        typeBeanAdater = new TypeBeanSelectAdater(getContext(), typeBeanList);
        typeGv.setAdapter(typeBeanAdater);
    }

    private void initView(View view) {
        typeed = view.findViewById(R.id.frag_type_name);
        typeIv = view.findViewById(R.id.frag_type_iv);
        typeGv = view.findViewById(R.id.frag_type_gv);
        type_save = view.findViewById(R.id.type_save);
        type_save.setOnClickListener(this);

        if(paramBean) {
            typeIv.setImageResource(typeBean.getsImageId());
            typeed.setText(typeBean.getTypeName());
        }

    }

    /*让子类必须重写该方法*/
    public abstract void saveTypeBeantoDb(String typename);

    @Override
    public void onClick(View v) {
        String typename = typeed.getText().toString();
        switch (v.getId()){
            case R.id.type_save:
                saveTypeBeantoDb(typename);
                break;
        }
    }
}